//==============================================================================
//
//               Copyright (C) 2005  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2005-06-26 06:40:33 $ $Revision: 1.1 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/DBigDouble.h"

#include "DInc.h"
#include "DTest.h"


//-Wrappers--------------------------------------------------------------------

void DBigDouble_test(void)
{
#ifdef HAVE_DBIGDOUBLE
  DBigDouble *nr1     = [DBigDouble alloc];
  DBigDouble *nr2     = [DBigDouble alloc];
  DBigDouble *nr3     = [DBigDouble alloc];
  DBigDouble *nr4     = nil;
  DBigDouble *nr5     = [DBigDouble new];
  DText      *txt     = nil;
  char        parse[] = " 123.67e-45e12 ";
  char       *pntr    = parse;
  
  STARTTEST();
  
  [nr1 init];
  [nr2 init :-61.431242 :256];

  [nr3 init :256];
  [nr3 set :"3.14143123423234322343223432" : 10];
  
  nr4 = [nr3 copy];

  TEST([nr3 compare :nr4] == 0);

  
  TEST(![nr1 isNegative]);
  TEST( [nr2 isNegative]);
  TEST(![nr3 isNegative]);
  TEST(![nr4 isNegative]);
  
  TEST( [nr1 isZero]);
  TEST(![nr2 isZero]);
  TEST(![nr3 isZero]);
  TEST(![nr4 isZero]);
  
  [nr4 clear];
  
  TEST( [nr4 isZero]);
  
  [nr4 move :nr3];
  
  TEST([nr4 compare :nr3] == 0);
  
  TEST([nr1 get] == 0.0);
  TEST([nr2 get] == -61.431242);
  

  txt = [nr4 get :10 :5];
  
  TEST([txt icompare :"0.31414e1"] == 0);
  
  [txt free];
  
  TEST([nr1 set :"34.8967128475875903673405847858738562095023874e5" :10]);
  TEST([nr2 set :"-89.3458747924875230987023353524342424343e3" :10]);
  TEST([nr3 set :"98.1234757128641327894789003489175091844554e7" :10]);
  TEST([nr4 set :"-0.93497834987149871394791479817498748917132498147891234e6" :10]);


  [nr5 move :nr1]; // nr5 = nr1
  
  [nr5 add :nr4];  // nr5 += nr4

  txt = [nr5 get :10 :20];
  
  TEST([txt icompare :"0.25546929348872603228e7"] == 0);
  
  [txt free];


  [nr5 add :nr2 :nr3]; // nr5 = nr2 + nr3
  
  txt = [nr5 get :10 :20];
  
  TEST([txt icompare :"0.98114541125384884037e9"] == 0);
  
  [txt free];
  
  
  [nr5 move :nr1];
  
  [nr5 sub :nr4]; // nr5 -= nr4
  
  txt = [nr5 get :10 :20];
  
  TEST([txt icompare :"0.44246496346302577507e7"] == 0);
  
  [txt free];
  
  
  [nr5 sub :nr2 :nr3]; // nr5 = nr2 - nr3
  
  txt = [nr5 get :10 :20];
  
  TEST([txt icompare :"-0.98132410300343381542e9"] == 0);
  
  [txt free];

  
  [nr5 move :nr1];
  
  [nr5 mul :nr4]; // nr5 *= nr4
  
  txt = [nr5 get :10 :20];
  
  TEST([txt icompare :"-0.32627670994176974242e13"] == 0);
  
  [txt free];

  
  [nr5 mul :nr2 :nr3]; // nr5 = nr2 * nr3
  
  txt = [nr5 get :10 :20];
  
  TEST([txt icompare :"-0.87669277752452492129e14"] == 0);
  
  [txt free];

  
  [nr5 move :nr1];
  
  [nr5 div :nr2]; // nr5 /= nr2
  
  txt = [nr5 get :10 :20];

  TEST([txt icompare :"-0.39058001198866557727e2"] == 0);
  
  [txt free];

  
  [nr5 div :nr2 :nr3]; // nr5 = nr2 / nr3
  
  txt = [nr5 get :10 :20];

  TEST([txt icompare :"-0.91054535261202683602e-4"] == 0);
  
  [txt free];

  
  [nr2 abs];
  
  txt = [nr2 get :10 :20];
  
  TEST([txt icompare :"0.89345874792487523099e5"] == 0);
  
  [txt free];
  
  
  [nr2 negate];
  
  txt = [nr2 get :10 :20];
  
  TEST([txt icompare :"-0.89345874792487523099e5"] == 0);
  
  [txt free];

  
  [nr3 sqrt];
  
  txt = [nr3 get :10 :20];
  
  TEST([txt icompare :"0.31324666911695028076e5"] == 0);
  
  [txt free];

  
  [nr4 power :481];
  
  txt = [nr4 get :10 :20];
  
  TEST([txt icompare :"-0.90270895248754467079e2872"] == 0); // didn't check this one...
  
  [txt free];

  [nr1 set :8.5];
  [nr2 set :-8.5];
  
  [nr1 ceil];
  [nr2 ceil];
  
  TEST([nr1 get] == 9.0);
  TEST([nr2 get] == -8.0);

  
  [nr1 set :8.5];
  [nr2 set :-8.5];
  
  [nr1 floor];
  [nr2 floor];
  
  TEST([nr1 get] == 8.0);
  TEST([nr2 get] == -9.0);

  
  [nr1 set :8.5];
  [nr2 set :-8.5];
  
  [nr1 trunc];
  [nr2 trunc];
  
  TEST([nr1 get] == 8.0);
  TEST([nr2 get] == -8.0);
  
  // printf("nr4:%s\n", [[nr4 get :10 :20] cstring]);

  // Conversion
 
  TEST([nr5 fromString :&pntr] == 0);
  
  TEST(*pntr == 'e');
  
  txt = [nr5 get :10 :5];

  TEST([txt icompare :"0.12367e-42"] == 0);
  
  [txt free];
  
  
  [nr1 set :"8.9000000000000000e10" :10];
  [nr2 set :"8.9000000000000001e10" :10];
  
  TEST([nr1 compare :nr2] < 0);
  TEST([nr2 compare :nr1] > 0);
  TEST([nr2 compare :nr2] == 0);
        

  [nr1 free];
  [nr2 free];
  [nr3 free];
  [nr4 free];
  [nr5 free];

  STOPTEST();
#endif
}

